package cn.com.wxd.service.robot.tuling;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.service.robot.tuling.util.RetTallTypeCode;

/**
 * 图灵机器人请求与回复类
 * @version 1.0
 * @author WangXuDong
 */
public class TuLingRobot {
	private static final Logger log = LoggerFactory.getLogger(TuLingRobot.class);
	static String key; //key
	static String tulingSDK;  //增加知识SDK
	public TuLingRobot(){
		key = TuLingconfigInfo.getKey();
		tulingSDK = TuLingconfigInfo.getTuLingSDK();
		if(key.length()!=32){
			try {
				throw new Exception("图灵机器人key错误");
			} catch (Exception e) {
                log.error("图灵机器人key错误",e);
			}
			return;
		}
		if(tulingSDK.length()<5){
			try {
				throw new Exception("图灵机器人聊天SDK错误");
			} catch (Exception e) {
                log.error("图灵机器人聊天SDK错误",e);
			}
			return;
		}
	}
	/**
	 * 设置相应的参数回复对话
	 * @param info 用户发送的信息， 请求内容，编码方式为UTF-8 
	 * @param userid 用户名，此userid针对开发者自己的每一个用户 
	 * @param loc 位置信息，编码方式为UTF-8 如北京中关村
	 * @param lon 经度信息  东经116.234632（小数点后保留6位），需要写为116234632 
	 * @param lat 纬度信息  北纬40.234632（小数点后保留6位），需要写为40234632 
	 * @return RobotTall机器人回复信息对象
	 * @throws Exception 
	 */
	public RobotTall reply(String info,String userid,String loc,
			String lon, String lat) throws Exception {
		String APIKEY = key; // 
		String INFO = URLEncoder.encode(info, "utf-8");
		String USERID = URLEncoder.encode(userid, "utf-8");
		String LON = URLEncoder.encode(lon, "utf-8");
		String LOC = URLEncoder.encode(loc, "utf-8");
		String LAT = URLEncoder.encode(lat, "utf-8");
		String getURL = tulingSDK+"?key=" + APIKEY
				+ "&info=" + INFO + "&userid=" + USERID + "&lon=" + LON
				+ "&loc=" + LOC + "&lat=" + LAT;
		URL getUrl = new URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		connection.connect();
		// 取得输入流，并使用Reader读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));
		String sb = new String();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb+=line;
		}
		reader.close();
		// 断开连接
		connection.disconnect();
		if (sb == null || sb.toString() == "") {
			sb="{\"code\":100000,\"text\":\"网络波动太大了，我看不清！\"}";
		}
		return RetTallTypeCode.ConvertStringToRobotall(sb);

	}
	/**
	 * 设置相应的参数回复对话
	 * @param info 用户发送的信息， 请求内容，编码方式为UTF-8 
	 * @param userid 用户名，此userid针对开发者自己的每一个用户 
	 * @param loc 位置信息，编码方式为UTF-8 如北京中关村
	 * @return RobotTall机器人回复信息对象
	 * @throws Exception 
	 */
	public RobotTall reply(String info,String userid,String loc) throws Exception {
		String APIKEY = key; // 
		String INFO = URLEncoder.encode(info, "utf-8");
		String USERID = URLEncoder.encode(userid, "utf-8");
		String LOC = URLEncoder.encode(loc, "utf-8");
		String getURL = tulingSDK+"?key=" + APIKEY
				+ "&info=" + INFO + "&userid=" + USERID
				+ "&loc=" + LOC;
		URL getUrl = new URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		connection.connect();
		// 取得输入流，并使用Reader读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));
		String sb = new String();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb+=line;
		}
		reader.close();
		// 断开连接
		connection.disconnect();
		if (sb == null || sb.toString() == "") {
			sb="{'code':100000,'text':'网络波动太大了，我看不清！'}";
		}
		return RetTallTypeCode.ConvertStringToRobotall(sb);


	}
	/**
	 * 设置相应的参数回复对话
	 * @param info 用户发送的信息， 请求内容，编码方式为UTF-8 
	 * @return RobotTall机器人回复信息对象
	 * @throws Exception 
	 */
	public RobotTall reply(String info) throws Exception {
		String APIKEY = key; // 
		String INFO = URLEncoder.encode(info, "utf-8");
		String getURL = tulingSDK+"?key=" + APIKEY
				+ "&info=" + INFO;
		URL getUrl = new URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		connection.connect();
		// 取得输入流，并使用Reader读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));
		String sb = new String();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb+=line;
		}
		reader.close();
		// 断开连接
		connection.disconnect();
		if (sb == null || sb.toString() == "") {
			sb="{'code':100000,'text':'网络波动太大了，我看不清！'}";
		}
		return RetTallTypeCode.ConvertStringToRobotall(sb);


	}
	/**
	 * 设置相应的参数回复对话
	 * @param info 用户发送的信息， 请求内容，编码方式为UTF-8 
	 * @param loc 位置信息，编码方式为UTF-8 如北京中关村
	 * @return RobotTall机器人回复信息对象
	 * @throws Exception 
	 */
	public RobotTall reply(String info,String loc) throws Exception {
		String APIKEY = key; // 
		String INFO = URLEncoder.encode(info, "utf-8");
		String LOC = URLEncoder.encode(loc, "utf-8");
		String getURL = tulingSDK+"?key=" + APIKEY
				+ "&info=" + INFO + "&loc=" + LOC;
		URL getUrl = new URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		connection.connect();
		// 取得输入流，并使用Reader读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));
		String sb = new String();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb+=line;
		}
		reader.close();
		// 断开连接
		connection.disconnect();
		if (sb == null || sb.toString() == "") {
			sb="{'code':100000,'text':'网络波动太大了，我看不清！'}";
		}
		return RetTallTypeCode.ConvertStringToRobotall(sb);


	}
}
